package com.itcast.googleplay.fragment;

import java.util.List;
import java.util.Random;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.itcast.googleplay.protocol.RecommendProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.ui.randomlayout.StellarMap;
import com.itcast.googleplay.utils.UIUtils;


public class RecommendFragment extends BaseFragment {
	private List<String> recommendData;

	@Override
	public ResultState onSubLoad() {
		RecommendProtocol recommendProtocol = new RecommendProtocol();
		recommendData = recommendProtocol.getData("recommend", 0, "");
		if(recommendData != null){
			if(recommendData.size()>0){
				return ResultState.STATE_SUCCESSED;
			}else{
				return ResultState.STATE_EMTPY;
			}
		}else{
			return ResultState.STATE_ERROR;
		}
	}

	@Override
	public View onCreateSuccessedSubView() {
		StellarMap stellarMap = new StellarMap(UIUtils.getContext());
		stellarMap.setRegularity(recommendData.size()-10, 10);
		stellarMap.setAdapter(new MyAdapter());
		int padding = UIUtils.dip2px(10);
		stellarMap.setInnerPadding(padding, padding, padding, padding);
		stellarMap.setGroup(0, true);
		return stellarMap;
	}
	
	class MyAdapter implements StellarMap.Adapter{

		@Override
		public int getGroupCount() {
			return 2;
		}

		@Override
		public int getCount(int group) {
			return recommendData.size();
		}

		@Override
		public View getView(int group, int position, View convertView) {
			TextView textView = new TextView(UIUtils.getContext());
			textView.setText(recommendData.get(position));
			int randomSp = new Random().nextInt(26)+14;
			
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, randomSp);
			int red = 30+new Random().nextInt(210);
			int green = 30+new Random().nextInt(210);
			int blue = 30+new Random().nextInt(210);
			
			int rgb = Color.rgb(red, green, blue);
			
			textView.setTextColor(rgb);
			
			return textView;
		}

		@Override
		public int getNextGroupOnPan(int group, float degree) {
			return 0;
		}

		@Override
		public int getNextGroupOnZoom(int group, boolean isZoomIn) {
			return 0;
		}
		
	}

}
